<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>7-基于Ajax的表单提交</title>
</head>
<body>
        <form id="userForm">
          <div>
            <label for="bookname">图书名称：</label>
            <input type="text" name="bookname" id="bookname">
          </div>
          <div>
            <label for="author">图书作者：</label>
            <input type="text" name="author" id="author">
          </div>
          <div>
            <label for="publisher">图书出版社：</label>
            <input type="text" name="publisher" id="publisher">
          </div>
          <div>
            <input type="submit" value="提交">
          </div>
        </form>
        <script type="text/javascript" src="./lib/jquery.js"></script>
        <script type="text/javascript">
      
          // 表单提交默认就会触发一个submit事件
          $('#userForm').on('submit', function (e) {
            // 阻止表单的默认提交行为
            e.preventDefault()
            var formData = $(this).serialize()
            $.post('http://www.liulongbin.top:3006/api/addbook', formData, function (res) {
              console.log(res)
            })
            // return false
          })
        </script>
      </body>
      </html>